package com.tem.mailguntest.utils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

@SuppressWarnings("all")
public class MailgunSendEmailByHttpClient {

	
	private static final String API_KEY = "key-dab9ec7a2edb35a027448e1032d0e55f";
	private static final String MESSAGES_URI = "https://api.mailgun.net/v3/sandbox84ab138276c84de6897e913e25e14c70.mailgun.org/messages";
	
	
	/**
	 * basic auth
	 *
	 * @author ganbo
	 * @date 2017年5月12日 下午5:02:46
	 * @param username
	 * @param password
	 * @return
	 */
	public static HttpClientContext addUserOAuth(String username,String password){
		HttpClientContext context = HttpClientContext.create();
        CredentialsProvider  credsProvider = new BasicCredentialsProvider();
        Credentials credentials = new UsernamePasswordCredentials(username,password);
        credsProvider.setCredentials(AuthScope.ANY,credentials);
        context.setCredentialsProvider(credsProvider);
        return context;
    }
	
	
	/**
	 * 发送普通邮件
	 *
	 * @author ganbo
	 * @date 2017年5月12日 下午6:43:29
	 */
	@Test
	public void sendEmail(){
		try {
			String targetURL = "https://api.mailgun.net/v3/sandbox84ab138276c84de6897e913e25e14c70.mailgun.org/messages";
			Map<String, String> params = new HashMap<String, String>();

			params.put("from", "ganbo@sandbox84ab138276c84de6897e913e25e14c70.mailgun.org");  //邮件来源ganbo@后面跟域名
			params.put("to", "946211284@qq.com");//收件者
			params.put("subject", "Hello, A Mailgun test subject");  //邮件主题
			params.put("html", "<html>Testing some Mailgun awesomness! 测试测试测试<p/><i>Bruce</i></html>"); //邮件内容

			// 服务器端响应地址
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(targetURL);
			MultipartEntity reqEntity = new MultipartEntity();

			// 其他参数
			if (params != null && params.size() != 0) {
				Set<String> keySet = params.keySet();
				for (String key : keySet) {
					StringBody value = new StringBody(params.get(key), ContentType.DEFAULT_TEXT);
					reqEntity.addPart(key, value);
				}
			}
			httppost.setEntity(reqEntity);
			HttpResponse responseBack = httpClient.execute(httppost,addUserOAuth("api", API_KEY));;
			// 响应返回值
			int statusCode = responseBack.getStatusLine().getStatusCode();
			HttpEntity resEntity = responseBack.getEntity();
			String result = EntityUtils.toString(resEntity, "UTF-8");// httpclient自带的工具类读取返回数据
			EntityUtils.consume(resEntity);
			System.out.println("result===>" + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 发送带附件的邮件
	 *
	 * @author ganbo
	 * @date 2017年5月12日 下午6:43:37
	 */
	@Test
	public void sendAttachmentEmail(){
		try {
			
			File attachment = new File("D:\\desktop\\图片\\1.jpg"); //附件
//			File attachment2 = new File("D:\\desktop\\图片\\2.jpg"); //附件
			
			String targetURL = "https://api.mailgun.net/v3/sandbox84ab138276c84de6897e913e25e14c70.mailgun.org/messages";
			Map<String, String> params = new HashMap<>();

			params.put("from", "ganbo@sandbox84ab138276c84de6897e913e25e14c70.mailgun.org");  //邮件来源ganbo@后面跟域名
			params.put("to", "946211284@qq.com");//收件者
			params.put("subject", "Hello, A Mailgun test subject");  //邮件主题
			params.put("html", "<html>Testing some Mailgun awesomness! 测试测试测试<p/><i>Bruce</i></html>"); //邮件内容

			// 服务器端响应地址
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(targetURL);
			MultipartEntity reqEntity = new MultipartEntity();
			FileBody fileBody = new FileBody(attachment);
//			FileBody fileBody2 = new FileBody(attachment2);
			//传递文件参数
			reqEntity.addPart("attachment", fileBody);
//			reqEntity.addPart("aaa", fileBody2);

			// 其他参数
			if (params != null && params.size() != 0) {
				Set<String> keySet = params.keySet();
				for (String key : keySet) {
					StringBody value = new StringBody(params.get(key), ContentType.DEFAULT_TEXT);
					reqEntity.addPart(key, value);
				}
			}
			httppost.setEntity(reqEntity);
			HttpResponse responseBack = httpClient.execute(httppost,addUserOAuth("api", API_KEY));;
			// 响应返回值
			int statusCode = responseBack.getStatusLine().getStatusCode();
			HttpEntity resEntity = responseBack.getEntity();
			String result = EntityUtils.toString(resEntity, "UTF-8");// httpclient自带的工具类读取返回数据
			EntityUtils.consume(resEntity);
			System.out.println("result===>" + result);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
